﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections;

namespace Scheduler.Data_Access_Layer
{
    /**Class: AcctManagement
    * @author Daniel Chopson
    * @version 1.0
    * Course : ITEC 4860 Spring 2012
    * Written: March 22, 2012
    *
    * This class connects to the User, Employee, and Manager tables in the database via
    * TableAdapters. Users, employees, and managers can be created. Getters and setters
    * are provided for users.
    *
    * Purpose: To provide the data access neccessary for creating and modifying user
    * accounts in the Account Management module.
    */

    class AcctManagement
    {
        //The user's ID

        private string userID;

        //The adapter for User table connectivity

        private SchedulerTableAdapters.UserTableAdapter userAdapter;

        //The data table for temporarily holding user data

        private Scheduler.UserDataTable userData;

        //A single row from the User table

        private Scheduler.UserRow userRow;

        //The adapter for Employee table connectivity

        private SchedulerTableAdapters.EmployeeTableAdapter employeeAdapter;

        //The adapter for Manager table connectivity

        private SchedulerTableAdapters.ManagerTableAdapter managerAdapter;

        /** Method: AcctManagement
          * Constructor for this class. Instantiates all class variables
          * @param userID the user's ID
          */

        public AcctManagement(string userID)
        {
            this.userID = userID;
            userAdapter = new SchedulerTableAdapters.UserTableAdapter();
            employeeAdapter = new SchedulerTableAdapters.EmployeeTableAdapter();
            managerAdapter = new SchedulerTableAdapters.ManagerTableAdapter();
        }

        /** Method: createUser
          * Creates a record in the User table
          * @param userIDToCreate the user's ID
          * @param password the user's password
          * @param isAcctLocked is the user's account locked
          * @param isAcctActive is the user's account active
          * @param name the user's name
          * @param phone the user's phone number
          * @param email the user's email address
          * @return an ArrayList of SQL error messages for error handling in the Presentation_Layer
          */

        public ArrayList createUser(string userIDToCreate, string password, bool isAcctLocked, bool isAcctActive, 
            string name, string phone, string email)
        {
            ArrayList errorMessages = new ArrayList();
            try 
            {
                userAdapter.Insert(userIDToCreate, password, isAcctLocked, isAcctActive, name, phone, email);
            }
            catch (SqlException exception)
            {
                for (int i = 0; i < exception.Errors.Count; i++)
                {
                    ErrorLookup errorLookup = new ErrorLookup(exception.Errors[i].Number);
                    errorMessages.Add(errorLookup.returnSimpleError());
                }
            }
            return errorMessages;
        }

        /** Method: createEmployee
          * Creates a record in the Employee table
          * @param payRate the employee's rate of pay
          * @param hireDate the date the employee was hired
          * @param empComment a comment field for the employee
          * @param mgrComment a comment field for the manager
          * @param empMinWeeklyHrs the employee's preference for minimum weekly hours
          * @param empMaxWeeklyHrs the employee's preference for maximum weekly hours
          * @param mgrMinWeeklyHrs the manager's preference for minimum weekly hours
          * @param mgrMaxWeeklyHrs the manager's preference for maximum weekly hours
          * @return an ArrayList of SQL error messages for error handling in the Presentation_Layer
          */

        public ArrayList createEmployee(Decimal payRate, DateTime hireDate, string empComment, string mgrComment,
            int empMinWeeklyHrs, int empMaxWeeklyHrs, int mgrMinWeeklyHrs, int mgrMaxWeeklyHrs, string preferencesLastUpdated)
        {
            ArrayList errorMessages = new ArrayList();
            try
            {
                employeeAdapter.Insert(userID, payRate, hireDate, empComment, mgrComment, empMinWeeklyHrs,
                empMaxWeeklyHrs, mgrMinWeeklyHrs, mgrMaxWeeklyHrs, preferencesLastUpdated);
            }
            catch (SqlException exception)
            {
                for (int i = 0; i < exception.Errors.Count; i++)
                {
                    ErrorLookup errorLookup = new ErrorLookup(exception.Errors[i].Number);
                    errorMessages.Add(errorLookup.returnSimpleError());
                }
            }
            return errorMessages;
        }

        /** Method: createManager
          * Creates a record in the Manager table
          * @param companyID the manager's company ID
          * @return an ArrayList of SQL error messages for error handling in the Presentation_Layer
          */

        public ArrayList createManager(string companyID)
        {
            ArrayList errorMessages = new ArrayList();
            try
            {
                managerAdapter.Insert(userID, companyID);
            }
            catch (SqlException exception)
            {
                for (int i = 0; i < exception.Errors.Count; i++)
                {
                    ErrorLookup errorLookup = new ErrorLookup(exception.Errors[i].Number);
                    errorMessages.Add(errorLookup.returnSimpleError());
                }
            }
            return errorMessages;
        }

        /** Method: locateUser
          * Locates the row in the User table with the specified user ID
          * Note: Must be called before the methods following it can be used
          * @return a boolean to denote whether the user was found
          */

        public bool locateUser()
        {
            userData = userAdapter.GetDataByUserID(userID);
            if (userData.Count == 0)
            {
                return false;
            }
            else
            {
                userRow = userData[0];
            }
            return true;
        }

        /** Method: getPassword
          * Gets the user's password
          * @return Password from the User table
          */

        public string getPassword()
        {
            return userRow.Password.Trim();
        }

        /** Method: setPassword
          * Sets the user's password
          * @param password the user's password
          */

        public void setPassword(string password)
        {
            userRow.Password = password;
            userAdapter.Update(userData);
        }

        /** Method: getIsAcctLocked
          * Gets the user's IsAcctLocked status
          * @return IsAcctLocked from the User table
          */

        public bool getIsAcctLocked()
        {
            return userRow.IsAcctLocked;
        }

        /** Method: setIsAcctLocked
          * Sets the user's IsAcctLocked status
          * @param isAcctLocked the user's IsAcctLocked status
          */

        public void setIsAcctLocked(bool isAcctLocked)
        {
            userRow.IsAcctLocked = isAcctLocked;
            userAdapter.Update(userData);
        }

        /** Method: getIsAcctActive
          * Gets the user's IsAcctActive status
          * @return IsAcctActive from the User table
          */

        public bool getIsAcctActive()
        {
            return userRow.IsAcctActive;
        }

        /** Method: setIsAcctActive
          * Sets the user's IsAcctActive status
          * @param isAcctActive the user's IsAcctActive status
          */

        public void setIsAcctActive(bool isAcctActive)
        {
            userRow.IsAcctActive = isAcctActive;
            userAdapter.Update(userData);
        }

        /** Method: getName
          * Gets the user's name
          * @return Name from the User table
          */

        public string getName()
        {
            return userRow.Name.Trim();
        }

        /** Method: setName
          * Sets the user's name
          * @param name the user's name
          */

        public void setName(string name)
        {
            userRow.Name = name;
            userAdapter.Update(userData);
        }

        /** Method: getPhone
          * Gets the user's phone number
          * @return Phone from the User table
          */

        public string getPhone()
        {
            return userRow.Phone.Trim();
        }

        /** Method: setPhone
          * Sets the user's phone number
          * @param phone the user's phone number
          */

        public void setPhone(string phone)
        {
            userRow.Phone = phone;
            userAdapter.Update(userData);
        }

        /** Method: getEmail
          * Gets the user's email address
          * @return Email from the User table
          */

        public string getEmail()
        {
            return userRow.Email.Trim();
        }

        /** Method: setEmail
          * Sets the user's email address
          * @param email the user's email address
          */

        public void setEmail(string email)
        {
            userRow.Email = email;
            userAdapter.Update(userData);
        }
    }
}
